package com.jw.server.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.ydx.protobuf.OTIpcDef;
import cn.ydx.protobuf.OTIpcDef.OTIpcList;

import com.jw.server.model.RatedDriver;
import com.jw.server.model.RatedDriverPunish;
import com.jw.server.model.RatedPassenger;
import com.jw.server.model.RatedPassengerComplaint;
import com.jw.server.service.RatedDriverPunishRepository;
import com.jw.server.service.RatedDriverRepository;
import com.jw.server.service.RatedPassengerComplaintRepository;
import com.jw.server.service.RatedPassengerRepository;
import com.jw.server.util.ByteUtil;

/**
 * 
 * @author xfm 2018年5月17日
 */

@RestController
@RequestMapping("/rated")
public class RateDataController {

	@Autowired
	RatedPassengerRepository ratedPassengerRepository;

	@Autowired
	RatedPassengerComplaintRepository ratedPassengerComplaintRepository;

	@Autowired
	RatedDriverPunishRepository ratedDriverPunishRepository;

	@Autowired
	RatedDriverRepository ratedDriverRepository;

	@PostMapping("/passengercomplaint")
	public String passengercomplaint(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<RatedPassengerComplaint> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.RatedPassengerComplaint> li = o
						.getRatedPassengerComplaintList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.RatedPassengerComplaint> l = li
							.iterator();
					basepay = new ArrayList<RatedPassengerComplaint>();
					while (l.hasNext()) {
						OTIpcDef.RatedPassengerComplaint base = l.next();
						basepay.add(RatedPassengerComplaint.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收ratedPassenger表的数据条数："+basepay.size());
			ratedPassengerComplaintRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "200";
	}

	@PostMapping("/driverpunish")
	public String driverpunish(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<RatedDriverPunish> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.RatedDriverPunish> li = o
						.getRatedDriverPunishList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.RatedDriverPunish> l = li.iterator();
					basepay = new ArrayList<RatedDriverPunish>();
					while (l.hasNext()) {
						OTIpcDef.RatedDriverPunish base = l.next();
						basepay.add(RatedDriverPunish.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收ratedDriverPunish表的数据条数："+basepay.size());
			ratedDriverPunishRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/driver")
	public String ratedDriver(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<RatedDriver> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.RatedDriver> li = o.getRatedDriverList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.RatedDriver> l = li.iterator();
					basepay = new ArrayList<RatedDriver>();
					while (l.hasNext()) {
						OTIpcDef.RatedDriver base = l.next();
						basepay.add(RatedDriver.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收ratedDriver表的数据条数："+basepay.size());
			ratedDriverRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/passenger")
	public String ratedPassenger(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<RatedPassenger> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.RatedPassenger> li = o.getRatedPassengerList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.RatedPassenger> l = li.iterator();
					basepay = new ArrayList<RatedPassenger>();
					while (l.hasNext()) {
						OTIpcDef.RatedPassenger base = l.next();
						basepay.add(RatedPassenger.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收ratedPassenger表的数据条数："+basepay.size());
			ratedPassengerRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

}
